//
//  MyCompanyViewController.m
//  reSearchDemo
//
//  Created by helfy  on 15-4-16.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MyCompanyViewController.h"
#import "KLocatePickView.h"
#import "KLocation.h"
@interface MyCompanyViewController ()
{
    
    NSDictionary *addressDic;
    
    NSString *province;
    NSString *city;
    NSString *region;
}
@end

@implementation MyCompanyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    self.isSectionMode = NO;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setCompanyInfo:(NSDictionary *)address
{
    addressDic = address;
    province=   addressDic[@"province"];
    city=addressDic[@"town"];
    region =addressDic[@"region"];
    if(address == nil)
    {
        self.title = @"新增公司资料";
    }
    else
    {
        self.title = @"修改公司资料";
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if(actionSheet.tag == 200)
    {
        
        if(buttonIndex == 1) {
            KLocatePickView *locateView = (KLocatePickView *)actionSheet;
            KLocation *location = locateView.locate;
            province = location.state;
            city = location.city;
            region = @" ";
            YMParameterCellObj *obj = ((KLocatePickView*)actionSheet).userInfo;
            obj.value = [NSString stringWithFormat:@"%@ %@",province,city];
            [self.tableView reloadData];
        }
    }
}
-(void)setupData
{
    [super setupData];
    
    YMParameterCellObj *obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
    obj.title = @"公司名称";
    obj.cellHeigth =45;
    obj.isRequired = YES;
    obj.key = @"companyname";
    if(addressDic)obj.value = addressDic[obj.key];
    
    UITextField *textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"公司全称";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
    obj.title = @"职位";
    obj.isRequired = YES;
    obj.key = @"position";
    if(addressDic)obj.value = addressDic[obj.key];
    
    obj.cellHeigth =45;
    textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"职位";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
    obj.title = @"手机号码";
    obj.cellHeigth =45;
    obj.isRequired = YES;
    obj.key = @"phone";
    if(addressDic)obj.value = addressDic[obj.key];
    obj.regex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    
    textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"11位手机号";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
    obj.title = @"选择地区";
    obj.cellHeigth =45;
    UILabel*lable = (UILabel *)obj.accessoryView;
    lable.text = @"地区信息";
    lable.textAlignment= NSTextAlignmentLeft;
    lable.textColor =[UIColor grayColor];
    obj.cellAction = @selector(showLoctionPicker:);
    if(addressDic)obj.value = [NSString stringWithFormat:@"%@ %@ %@",addressDic[@"province"],addressDic[@"town"],addressDic[@"region"]];
    
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
    obj.title = @"详细地址";
    obj.cellHeigth =60;
    textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"街道门牌信息";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    obj.isRequired = YES;
    obj.key = @"address";
    if(addressDic)obj.value = addressDic[obj.key];
    
    [self.cellObjs addObject:obj];
    
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextView];
    obj.title = @"公司简介";
    obj.cellHeigth =100;
    obj.isRequired = YES;
    obj.key = @"introduce";
    if(addressDic)obj.value = addressDic[obj.key];

    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
}



-(void)showLoctionPicker:(YMParameterCellObj*)obj
{
    
    [self.view endEditing:YES];
    KLocatePickView *locateView = [[KLocatePickView alloc] initWithTitle:@"选择城市" delegate:self];
    locateView.tag = 200;
    locateView.userInfo = obj;
    [locateView showInView:self.view];
}

-(void)save
{
    
    [self.view endEditing:YES];
    for (YMParameterCellObj *obj in self.cellObjs) {
        NSString *errorStr = [obj check];
        if (errorStr.length >0) {
            [self showText:errorStr];
            return;
        }
    }
    
    if ([super startRequest]) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        for (YMParameterCellObj *obj in self.cellObjs) {
            if(obj.key && obj.value)
            {
                [dic setObject:obj.value forKey:obj.key];
            }
        }
        [dic setObject:[[BSEngine currentEngine] user].uid forKey:@"uid"];
        if(addressDic)
        {
            [dic setObject:[addressDic objectForKey:@"companyid"] forKey:@"companyid"];
        }
        
        [dic setValue:province forKey:@"province"];
        [dic setValue:city forKey:@"town"];
        [dic setValue:region forKey:@"region"];
        
        [client requestFor:dic methodName:@"User/Api/setMycompany"];
    }
}

- (BOOL)requestDidFinish:(id)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
        NSDictionary *data = obj[@"data"];
        if([data isKindOfClass:[NSDictionary class]])
        {
            NSString *debugMsg = data[@"msg"];
            [self showText:debugMsg];
        }
        else{
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    }
    return NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
